package com.example.travelsn.homepage;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.travelsn.R;
import com.example.travelsn.db.DBManager;
import com.example.travelsn.db.Feedback;
import com.luluteam.itestlib.apm.test.tool.FragmentProxy;

public class UserFragment extends Fragment {
    private Button bFeedback;
    private EditText etFeedback;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FragmentProxy.callonCreate(this);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_user, container, false);
    }
    @Override
    public void  onActivityCreated(Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);
        initEvent();
        bFeedback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //信息反馈
                String sFeedback = etFeedback.getText().toString();
                if(!sFeedback.equals("") && sFeedback != null){
                    DBManager dbManager = new DBManager(getContext());
                    System.out.println(sFeedback);
                    Feedback feedback = new Feedback();
                    feedback.setContent(sFeedback);
                    dbManager.addFeed(feedback);
                    Toast.makeText(getActivity(),"反馈成功",Toast.LENGTH_SHORT).show();
                    etFeedback.setText("");
                    InputMethodManager imm = ( InputMethodManager ) v.getContext( ).getSystemService( Context.INPUT_METHOD_SERVICE );
                    //关闭软键盘
                    if ( imm.isActive( ) ) {
                        imm.hideSoftInputFromWindow( v.getApplicationWindowToken( ) , 0 );
                    }
                }
                else
                    new AlertDialog.Builder(getContext()).setTitle("输入有误")
                        .setMessage("请输入反馈内容")
                        .setIcon(android.R.drawable.ic_dialog_info)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                            }
                        }).show();
            }
        });
    }
    @Override
    public void onResume() {
        super.onResume();
        FragmentProxy.callonResume(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        FragmentProxy.callonPause(this);
    }
    private void initEvent(){
        bFeedback = (Button)getActivity().findViewById(R.id.b_userfeed);
        etFeedback = (EditText)getActivity().findViewById(R.id.et_userfeed);
    }
}
